package com.ywwxhz.lib.kits;

import java.io.File;
import java.util.Map;

import android.util.Log;

import org.apache.commons.lang3.StringUtils;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import com.alibaba.fastjson.JSONObject;
import com.ywwxhz.common.Constants;
import com.ywwxhz.lib.Configure;

/**
 * By HH
 */
public class HttpUtil {
    public static String TAG = HttpUtil.class.getName();

    private HttpUtil() {
    }

    public static <T> void post(String url, JSONObject json, Callback.ProgressCallback<T> responseHandler) {
        RequestParams params = createParams(url);
        params.setAsJsonContent(true);
        String res = json.toString();
        params.setBodyContent(res);
        Log.d(TAG, res);
        x.http().post(params, responseHandler);
    }

    public static <T> void postMulti(String url, Map<String, Object> params, Callback.ProgressCallback<T> responseHandler) {
        RequestParams pm = createParams(url);
        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() instanceof File) {
                    pm.addBodyParameter(entry.getKey(), (File) entry.getValue());
                    pm.setMultipart(true);
                } else {
                    pm.addBodyParameter(entry.getKey(), String.valueOf(entry.getValue()));
                }
            }
        }
        x.http().post(pm, responseHandler);
    }

    public static <T> void get(String url, Map<String, String> params, Callback.ProgressCallback<T> responseHandler) {
        RequestParams pam = createParams(url);
        if (params != null) {
            Log.d(TAG, params.toString());
            for (Map.Entry<String, String> entry : params.entrySet()) {
                pam.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        x.http().get(pam, responseHandler);
    }

    public static Object postSync(String url, JSONObject json) {
        RequestParams params = createParams(url);
        params.setAsJsonContent(true);
        params.setBodyContent(json.toString());
        try {
            return x.http().postSync(params, Object.class);
        } catch (Throwable throwable) {
            Log.e(HttpUtil.class.getName(), "请求出错！", throwable);
            return null;
        }
    }

    public static RequestParams createParams(String url) {
        String fullUrl = (StringUtils.startsWithIgnoreCase(url, "HTTP://") || StringUtils.startsWithIgnoreCase(url, "HTTPS://")) ? url : (Configure.BASE_URL + url);
        Log.d(TAG, fullUrl);
        RequestParams params = new RequestParams(fullUrl);
        params.setCancelFast(true);
        params.setAutoResume(true);
        params.setCharset("utf-8");
        params.setMaxRetryCount(3);
        params.setHeader("Referer", Configure.BASE_URL);
        params.setHeader("Origin", Configure.BASE_URL);
        params.setHeader("X-Requested-With", "XMLHttpRequest");
        params.setHeader("User-Agent", Constants.USER_AGENT);
        params.setUseCookie(true);
        return params;
    }

}
